package com.ljc.cjgl.service;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.entitiy.Student;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.form.StudentForm;
import com.ljc.cjgl.repository.ClassRepository;
import com.ljc.cjgl.repository.StudentRepository;

@Service
public class StudentService {
	@Autowired
	private StudentRepository studentDAO;
	@Autowired
	private ClassRepository classDAO;
	public List<Student> findByTbClassId(Integer id){
		return studentDAO.findByTbClassId(id);
	}
	
	public Student findById(Integer id) {
		Optional<Student> optional = studentDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	
	public Page<Student> findAll(Specification<Student> spec,Pageable pageable){
		return studentDAO.findAll(spec, pageable);
	}
	
	public void save(Student model) {
		studentDAO.save(model);
	}
	
	public void save(StudentForm form) {
		Student student=new Student();
		if(form.getId()!=null) {
			student=findById(form.getId());
		}
		BeanUtils.copyProperties(form, student,"id");
		studentDAO.save(student);
	}
	
	public Boolean delete(Integer id) {
		try {
			studentDAO.deleteById(id);
			return true;
		} catch (Exception e) {
			return false;
		} 
	}

	public void save(MultipartFile file, StudentForm form) throws IOException {
		Student student=new Student();
		Integer id = form.getId();
		if(id!=null) {
			student=findById(id);
		}
		BeanUtils.copyProperties(form, student,"id","photoName","saveName");
		String fileName = file.getOriginalFilename();
		if(StringUtils.hasText(fileName)) {
	    	student.setPhotoName((new File(fileName)).getName());
	    	String uid=UUID.randomUUID().toString();
	    	student.setSaveName(uid);
	    	FileUtil.uploadFile(file.getBytes(), uid);
		}
		studentDAO.save(student);
	}

	public List<TbClass> findByTbClassNameLike(String bjMc) {
		return classDAO.findByNameLike("%"+bjMc+"%");
	}

	public List<TbClass> findAllTbClass() {
		return classDAO.findAll();
	}

	public Student findByUid(String uid) {
		Optional<Student> optional = studentDAO.findBySaveName(uid);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	
	@Transactional
	public synchronized boolean delete(List<Student> students) {
		try {
			for(Student s:students) {
				String saveName = s.getSaveName();
				studentDAO.deleteById(s.getId());
				if(StringUtils.hasText(saveName)) {
					File file=new File(FileUtil.getFilePath(), saveName);
					if(file.exists()) {
						file.delete();
					}
				}
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<Student> findAll(Sort sort) {
		return studentDAO.findAll(sort);
	}

	public Student findByStuNumAndPwd(String userName, String pwd) {
		List<Student> list=studentDAO.findByStuNumAndPwd(userName,pwd);
		if(!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	
	
}
